﻿using System;
using System.Threading;

namespace CSharp进阶_多线程
{
	class Program
	{
		static bool isRunning = true;
		static object mutex = new object();
		#region 多线程
		//static void Main(string[] args)
		//{
		//	//Console.WriteLine("Thread {0}", Thread.GetCurrentProcessorId());
		//	#region 初始化
		//	Thread t1 = new Thread(() =>
		//	{
		//		Console.WriteLine("Thread {0}", Thread.GetCurrentProcessorId());
		//		int count = 0;
		//		while (count < 10)
		//		{
		//			Console.WriteLine("Thread 1, count: {0}", count++);
		//			Thread.Sleep(1000);
		//		}
		//		//int count = 0;
		//		//while (isRunning)
		//		//{
		//		//	Console.WriteLine("Thread 1, count: {0}", count++);
		//		//	Thread.Sleep(1000);
		//		//}
		//		Console.WriteLine("Thread {0} exit", Thread.GetCurrentProcessorId());

		//	});
		//	#endregion
		//	#region	运行
		//	//t1.Start();
		//	#endregion
		//	#region 设置为后台线程
		//	//t1.IsBackground = true;
		//	#endregion
		//	#region 线程退出
		//	// 1.通过flag
		//	//Console.ReadKey();
		//	//isRunning = false;
		//	// 2.通过Abort
		//	//Console.ReadKey();
		//	//t1.Abort();
		//	#endregion
		//	#region 线程等待
		//	//t1.Join();
		//	#endregion
		//	#region 线程休眠
		//	//Thread.Sleep(5000);
		//	#endregion
		//	#region 线程安全
		//	Console.CursorVisible = false;
		//	Thread t2 = new Thread(() =>
		//	{
		//		while (true)
		//		{
		//			lock (mutex)
		//			{
		//				Console.SetCursorPosition(0, 0);
		//				Console.ForegroundColor = ConsoleColor.Red;
		//				Console.WriteLine("■");
		//			}
		//		}
		//	});
		//	Thread t3 = new Thread(() =>
		//	{
		//		while(true)
		//		{
		//			lock (mutex)
		//			{
		//				Console.SetCursorPosition(2, 2);
		//				Console.ForegroundColor = ConsoleColor.Yellow;
		//				Console.WriteLine("●");
		//			}
		//		}
		//	});
		//	t2.Start();
		//	t3.Start();
		//	t2.Join();
		//	t3.Join();
		//	#endregion
		//	//Console.ReadKey();
		//	Console.WriteLine("Thread {0} exit", Thread.GetCurrentProcessorId());
		//}
		#endregion
		#region 贪吃蛇
		enum E_MoveDirection
		{
			Up,
			Down,
			Left,
			Right
		}
		class Icon
		{
			public int x;
			public int y;
			public string icon = "块";
			public E_MoveDirection moveDirection = E_MoveDirection.Right;
			public Icon(int x, int y, E_MoveDirection moveDirection = E_MoveDirection.Right)
			{
				this.x = x;
				this.y = y;
				this.moveDirection = moveDirection;
			}
			public void Show()
			{
				Console.SetCursorPosition(x, y);
				Console.ForegroundColor = ConsoleColor.Red;
				Console.Write(icon);
			}
			public void Hide()
			{
				Console.SetCursorPosition(x, y);
				Console.Write(" ");
			}
			public void Move()
			{
				Hide();
				switch (moveDirection)
				{
					case E_MoveDirection.Up:
						y--;
						break;
					case E_MoveDirection.Down:
						y++;
						break;
					case E_MoveDirection.Left:
						x-=2;
						break;
					case E_MoveDirection.Right:
						x+=2;
						break;
				}
				Show();
			}
			public void ChangeDirection(E_MoveDirection newDirection)
			{
				moveDirection = newDirection;
			}
		}
		static void Main(string[] args)
		{
			Icon icon = new Icon(20, 10);
			icon.Show();
			Thread readThread = new Thread((icon) =>
			{
				while(isRunning)
				{
					ConsoleKey select = Console.ReadKey(true).Key;
					switch (select)
					{
						case ConsoleKey.W:
							(icon as Icon).ChangeDirection(E_MoveDirection.Up);
							break;
						case ConsoleKey.S:
							(icon as Icon).ChangeDirection(E_MoveDirection.Down);
							break;
						case ConsoleKey.A:
							(icon as Icon).ChangeDirection(E_MoveDirection.Left);
							break;
						case ConsoleKey.D:
							(icon as Icon).ChangeDirection(E_MoveDirection.Right);
							break;
					}
				}
				Console.Write("read thread exit already");
			});
			readThread.IsBackground = false;
			readThread.Start(icon);
			Console.CursorVisible = false;
			int count = 0;
			while (true)
			{
				Thread.Sleep(500);
				icon.Hide();
				icon.Move();
				icon.Show();
				count++;
				if (count > 5)
				{
					isRunning = false;
					break;
				}
			}
			Console.Write("按任意键结束游戏> ");
			Console.ReadKey();
		}
		#endregion
	}
}
